<template>
  <div class="app-container">
    <!-- 添加或修改项目基础信息管理对话框 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="生产单号" prop="produceNo">
        <el-input v-model="queryParams.produceNo" placeholder="请输入生产单号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="订单编号" prop="projectNo">
        <el-input v-model="queryParams.projectNo" placeholder="请输入订单编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品编码" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入产品编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="工程名称" prop="gongchengName">
        <el-input v-model="queryParams.gongchengName" placeholder="请输入工程名称" clearable
                  @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="需求日期" prop="saleDate">
        <el-date-picker clearable v-model="queryParams.saleDate" type="date" value-format="yyyy-MM-dd"
                        placeholder="请选择需求日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="产品名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入产品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品型号" prop="model">
        <el-input v-model="queryParams.model" placeholder="请输入产品型号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <!--<el-button type="warning" plain icon="el-icon-download" size="mini"
                   @click="handleExportDetail" >导出
        </el-button>-->
      </el-form-item>
    </el-form>
    <!--<el-table :data="dataList" :max-height="tableMaxHeight" @selection-change="handleSelectionChange"
              v-loading="loading">-->
    <el-col :span="1.5">
      <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="multiple"
        @click="piliangFenpeiGx">批量串行拆解</el-button>
    </el-col>
    <el-table border :data="columns" ref="dragTable" row-key="id" v-loading="loading" @selection-change="handleSelectionChange"
      @sort-change="handleSortChange" :max-height="tableMaxHeight">
      <el-table-column align="center" type="selection" width="55" />
      <el-table-column align="center" label="序号" type="index" width="50">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column sortable label="生产单号" align="center" prop="produceNo" />
      <el-table-column sortable label="订单编号" align="center" prop="projectNo"  show-overflow-tooltip/>
      <el-table-column sortable label="客户名称" align="center" prop="customName" show-overflow-tooltip />
      <el-table-column sortable label="工程名称" align="center" prop="gongchengName" show-overflow-tooltip />
      <el-table-column sortable label="产品编码" align="center" prop="code" />
      <!--<template slot-scope="scope">
        <span>{{ scope.row.code }}</span>
      </template>
    </el-table-column>-->
      <el-table-column sortable align="center" label="产品名称" prop="name" show-overflow-tooltip />
      <el-table-column sortable align="center" label="产品型号" prop="xinghao" />
      <!--<el-table-column sortable align="center" label="订单数量" prop="orderCount" />
      <el-table-column sortable align="center" label="锁定数量" prop="lockCount" />-->
      <el-table-column align="center" label="自制数量" prop="count" />
      <!--<el-table-column align="center" label="巡检单" prop="xunjianFile" width="200">
        <template slot-scope="scope" v-if="scope.row.xunjianFile">
          <image-preview :src="scope.row.xunjianFile" height="60px" width="40px"></image-preview>
        </template>
      </el-table-column>-->
      <el-table-column label="下单日期" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="需求日期" align="center" prop="saleDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.saleDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="产品描述" prop="miaoshu" show-overflow-tooltip />
      <el-table-column align="center" label="备注" prop="remark" show-overflow-tooltip />
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <!--<el-button v-if="scope.row.ischaijie == 0" @click="chaijie(scope.row)" icon="el-icon-view" size="mini" type="text">
            串行拆解
          </el-button>-->
          <el-button @click="chaijie(scope.row)" icon="el-icon-view" size="mini" type="text">
            串行拆解
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <choseGx-form @ok="getList" ref="choseGxForm"></choseGx-form>
    <!-- <piliangFenpeiGxp-form @ok="getList" ref="piliangFenpeiGxpForm"></piliangFenpeiGxp-form> -->
    <piliangChaijie-form @ok="getList" ref="piliangChaijieForm"></piliangChaijie-form>
    <chajie-form @ok="getList" ref="chaijieForm"></chajie-form>
    <fpTaskForm-list @ok="getList" ref="taskFpForm"></fpTaskForm-list>
    <xunjian-form @ok="getList" ref="xunjianForm"></xunjian-form>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import {
  addAction,
  deleteAction,
  downloadFile,
  getAction,
  getById
} from '@/api/manage'
import PiliangChaijieForm from "@/views/develop/projectBaseInfo/prjForGoodsIn/PiliangChaijieForm";
import ChoseGxForm from "@/views/develop/projectBaseInfo/prjForGoodsIn/ChoseGxForm";
import ChajieForm from "@/views/develop/projectBaseInfo/prjForGoodsIn/ChajieForm";
import FpTaskFormList from "@/views/develop/projectBaseInfo/prjForGoodsIn/FpTaskFormList";
import XunjianForm from "@/views/develop/projectBaseInfo/prjForGoodsIn/XunjianForm";

export default {
  name: "FenpeiCjList",
  dicts: ['isdo', 'sale_state', 'goods_status', 'ischaijie'],
  components: {
    ChoseGxForm,
    PiliangChaijieForm,
    ChajieForm,
    FpTaskFormList,
    XunjianForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: "设定产品物料",
      // 是否显示弹出层
      open: false,
      single: true,
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 表单参数
      form: {},
      proId: "",
      proStatus: "",
      columns: [],
      total: 0,
      ids: [],
      goodsIds: [],
      // 表单校验
      rules: {},
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        params: {},
        id: null,
        code: null,
        projectNo: null,
        name: null,
        model: null,
        xinghao: null,
        produceNo: null,
        saleDate: null,
        //isdo: "1",//自制的
        ischaijie: '0',
        //state: "0",//未分配工艺工序
        remark: null,
        projectId: null,
        orderByColumn: "create_time",
        isAsc: "desc"
      },
      url: {
        list: '/factoryProduceSaleAll/factoryProduceSaleAll/list',
        getById: '/projectBaseInfo/projectBaseInfo/',
        exportData: '/goodsSale/goodsSale/exportData',
      },
      tableMaxHeight: window.innerHeight - 320,
    };
  },
  created() {
    //this.handleQuery();
    this.getList();
    window.addEventListener('resize', this.getTableHeight);
  },
  methods: {
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.handleQuery();
    },
    xunjian(row) {
      this.$refs.xunjianForm.xunjianShow(row);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.goodsIds = selection.map(item => item.goodsId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    piliangFenpeiGx() {
      if (this.goodsIds.length > 0) {
        const firstGoodsId = this.goodsIds[0];
        const allSame = this.goodsIds.every(goodsId => goodsId === firstGoodsId);
        if (!allSame) {
          this.$message.warning('选择的产品编码不一致，请重新选择');
        } else {
          this.$refs.piliangChaijieForm.chaijieGoods(this.ids, this.goodsIds);
        }
      }
    },
    taskFp(row) {
      this.$refs.taskFpForm.taskFpShow(row);
    },
    chaijie(row) {
      this.$refs.chaijieForm.chaijieGoods(row);
    },
    choseGy(row) {
      this.$refs.choseGxForm.choseGy(row);
    },
    getList() {
      this.loading = true;
      getAction(this.url.list, this.queryParams).then(response => {
        this.columns = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.params.salesmanName = ''
      this.queryParams.params.orderDate = ''
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    reset() {
      this.form = {
        id: null,
        remark: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },

    handleExportDetail(row) {
      downloadFile(this.url.exportData, this.queryParams);
    },

  }
};
</script>
